from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
import unittest
import json
import os, stat
import random
import requests
import sys, getopt
from selenium.webdriver import ActionChains
import argparse
from base64 import b64encode
from selenium.webdriver.common.proxy import ProxyType, Proxy
import string
import zipfile
from selenium.webdriver.common.keys import Keys
import pyautogui
import shutil
import platform
import pyperclip
from docx import Document
from docx.shared import Cm
import PyPDF2
file_name = ''
try:
    argv = sys.argv[1:]
    file_name = argv[1]
except:
    pass
MY_USER_AGENT = [
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:86.0) Gecko/20100101 Firefox/86.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
    "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
    "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
    "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
    "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
    "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:68.0) Gecko/20100101 Firefox/68.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
    "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
    "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
    "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
    "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
    "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
    "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
    "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
    "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
]
download_path = (os.getcwd()+'/downloads').replace('\\','/')
download_path = 'C:/Users/Lenovo/Downloads'
#谷歌浏览器访问处理标书查询
class VisitChrome(unittest.TestCase):
    url = ''
    file_name = './index.txt'
    company={
        "name":""
    }
    fail_num=0
    credit_url='https://www.creditchina.gov.cn/xinyongfuwu/?navPage=4' #信用中国查询地址
    credit_home_url='https://www.creditchina.gov.cn/'
    ccgp_url='http://www.ccgp.gov.cn/search/cr/' #中国政府采购网
    wenshu_url='https://wenshu.court.gov.cn' #中国裁判文书网
    xwqy_url='http://xwqy.gsxt.gov.cn/'#小微企业查询
    fail_allow=3
    host=''
    port=''
    urls = [
       "https://www.creditchina.gov.cn/xinyongfuwu/shixinheimingdan/",
       'https://www.creditchina.gov.cn/xinyongfuwu/shixinbeizhixingrenchaxun/',
       'https://www.creditchina.gov.cn/xinyongfuwu/zhongdashuishouweifaanjian/',
       'https://www.creditchina.gov.cn/xinyongfuwu/zhengfucaigouyanzhongweifashixinmingdan/'
    ]
    config={
        "notice": {
            "url": "",
            "email": "",
            "password": ""
        },
        "wen_shu": {
            "username": "",
            "password": ""
        },
        "show_phone":[]
    }
    config_file = './config.json'
    login_wen_shu_ed = 0
    login_email_ed = 0
    index = 0
    datas = []
    datas1=[]
    def pdfIsOk(self,file_name):
        try:
            PyPDF2.PdfFileReader(open(file_name, "rb"))
        except PyPDF2.utils.PdfReadError:
            print("PDF文件未下载完成")
            return False
        except OSError:
            print("不是PDF文件")
            return False
        else:
            pass
        return True
    def setUp(self):
        #读取配置信息
        if os.path.exists(self.config_file):
            with open(self.config_file, 'r', encoding='utf-8') as f:  # 打开文件
                try:
                    config = json.loads(f.read())
                    if config:
                        self.config = config
                except:
                    print('读取配置文件异常')
        self.setFirefox()
        if file_name:
            self.file_name = file_name
    def loginWenShu(self):
        login_url='https://wenshu.court.gov.cn'
        driver = self.driver
        self.driver.get(login_url)
        time.sleep(2)
        aEl = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,"#loginLi > a:nth-child(1)"))
        )
        aEl.click()
        time.sleep(2)
        try:
            iframe = WebDriverWait(self.driver,10).until(
                EC.presence_of_element_located((By.ID,'contentIframe'))
            )
        except:
            login_url = 'https://wenshu.court.gov.cn/website/wenshu/181010CARHS5BS3C/index.html?open=login'
            self.driver.get(login_url)
            time.sleep(2)
        iframe = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.ID,'contentIframe'))
        )
        self.driver.switch_to.frame(iframe)
        try:
            input = WebDriverWait(driver,10).until(
                EC.presence_of_element_located((By.CLASS_NAME,"phone-number-input"))
            )
        except:
            login_url = 'https://wenshu.court.gov.cn/website/wenshu/181010CARHS5BS3C/index.html?open=login'
            self.openUrl(url=self.wenshu_url,index=0)
            time.sleep(2)
            iframe = WebDriverWait(self.driver,10).until(
                EC.presence_of_element_located((By.ID,'contentIframe'))
            )
            self.driver.switch_to.frame(iframe)
            input = WebDriverWait(driver,10).until(
                EC.presence_of_element_located((By.CLASS_NAME,"phone-number-input"))
            )
        input.send_keys(self.config['wen_shu'].get('username',''))
        password = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,"#root .password-input .password"))
        )
        password.send_keys(self.config['wen_shu'].get('password',''))
        submitBtn = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".login-button-container .button"))
        )
        submitBtn.click()
        time.sleep(3)
        self.driver.switch_to.default_content()
        #等待确认是否登录成功
        while True:
           time.sleep(0.5)
           if self.isElementPresent("css selector", '.searchKey') or (self.isElementPresent("css selector", '#logout') and '退出' in self.driver.find_element(by='id', value='logout').text):
              break
        self.login_wen_shu_ed = 1
        time.sleep(1)
    #登录QQ邮箱
    def loginEmail(self):
        driver = self.driver
        self.driver.get(self.config['notice'].get('url','https://mail.qq.com/'))
        try:
            iframe = WebDriverWait(self.driver,10).until(
                EC.presence_of_element_located((By.ID,'login_frame'))
            )
            self.driver.switch_to.frame(iframe)
            login = WebDriverWait(driver,10).until(
                EC.presence_of_element_located((By.ID,'login'))
            )
            switcher_plogin = WebDriverWait(driver,10).until(
                EC.presence_of_element_located((By.ID,'switcher_plogin'))
            )
            #账号密码登录
            if switcher_plogin.is_displayed() is False:
                input = WebDriverWait(driver,10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,"#loginform #u"))
                )
                input.send_keys(self.config['notice'].get('email',''))
                password = WebDriverWait(driver,10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,"#loginform #p"))
                )
                password.send_keys(self.config['notice'].get('password',''))
                submitBtn = WebDriverWait(driver,10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,"#login_button"))
                )
                time.sleep(0.5)
                submitBtn.click()
            elif self.isElementPresent("css selector", '#qlogin_list a'):
                loginA = self.driver.find_element(by='css selector', value='#qlogin_list a')
                loginA.click()
            time.sleep(2)
        except Exception as err:
            #print(err)
            pass
        self.driver.switch_to.default_content()
        while True:
            time.sleep(0.5)
            if self.isElementPresent("css selector", '#SetInfo'):
               break
        self.login_email_ed = 1
    def setFirefox(self):
        #profile = webdriver.FirefoxProfile('C:/Users/Lenovo/AppData/Roaming/Mozilla/Firefox/Profiles/profiles')
        #profile = webdriver.FirefoxProfile()
        options = webdriver.FirefoxOptions()
        options.set_preference('browser.cache.disk.parent_directory', os.getcwd()+'/cache')
        options.set_preference("browser.link.open_newwindow",3)
        options.set_preference("browser.download.folderList",2)
        options.set_preference("browser.download.manager.showWhenStarting",False)
        options.set_preference("browser.helperApps.neverAsk.openFile", 'application/pdf')
        options.set_preference("browser.helperApps.neverAsk.saveToDisk",'application/pdf')
        options.set_preference("browser.helperApps.showOpenOptionForPdfJS",False)
        options.set_preference("browser.download.dir", download_path)
        #profile.update_preferences()
        #self.driver = webdriver.Firefox(firefox_profile=profile,options=options)
        self.driver = webdriver.Firefox(options=options)
        #try:
        #    self.driver = webdriver.Firefox(profile,options=options)
        #except:
        #    print('浏览器设置异常')
        #    try:
        #        time.sleep(random.randint(0,10))
        #        self.driver = webdriver.Firefox(profile,options=options)
        #    except:
        #        print('重试浏览器设置异常')
        self.driver.set_page_load_timeout(60)
        self.driver.get("about:addons")
        extension = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.XPATH,'/html/body/div/div[1]/categories-box/button[2]'))
        )
        extension.click()
        body = self.driver.find_element(by='css selector', value='body')
        body.send_keys(Keys.CONTROL + 't')
        body.send_keys(Keys.COMMAND + 't')
        time.sleep(0.5)
        self.driver.switch_to.window(self.driver.window_handles[1])
        self.driver.get('file://'+os.getcwd()+'/index.html')
        self.driver.switch_to.window(self.driver.window_handles[0])
        jieTuExtension = WebDriverWait(self.driver,60).until(
            EC.presence_of_element_located((By.XPATH,'//*[@addon-id="easyscreenshot@mozillaonline.com"]'))
        )
        self.driver.close()
        time.sleep(1)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.get("about:config")
        body = self.driver.find_element(by='css selector', value='body')
        time.sleep(1)
        warningButton = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.ID,"warningButton"))
        )
        warningButton.click()
        time.sleep(0.1)
        user_agent = random.choice(MY_USER_AGENT)
        js='''
        var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        prefs.setIntPref("browser.link.open_newwindow", 3);
        prefs.setIntPref("browser.download.folderList", 2);
        prefs.setBoolPref("browser.download.manager.showWhenStarting", false);
        prefs.setBoolPref("browser.helperApps.showOpenOptionForPdfJS", false);
        prefs.setBoolPref("browser.bookmarks.openInTabClosesMenu", false);
        prefs.setBoolPref("browser.download.alwaysOpenInSystemViewerContextMenuItem", false);
        prefs.setBoolPref("browser.download.openInSystemViewerContextMenuItem", false);
        prefs.setCharPref("browser.helperApps.neverAsk.openFile", 'application/pdf');
        prefs.setCharPref("browser.helperApps.neverAsk.saveToDisk", 'application/pdf');
        prefs.setBoolPref("pdfjs.enabledCache.state", false);
        //prefs.setBoolPref("pdfjs.disabled", true);
        prefs.setBoolPref("plugin.scan.plid.all", false);
        prefs.setCharPref("plugin.scan.Acrobat", "99.0");
        prefs.setCharPref("plugin.scan.wpsoffice", "99.0");
        prefs.setBoolPref("browser.helperApps.alwaysAsk.force", false);
        prefs.setBoolPref("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        prefs.setBoolPref("browser.download.useDownloadDir", true);
        prefs.setBoolPref("dom.webdriver.enabled", false);
        prefs.setBoolPref("useAutomationExtension", false);
        //prefs.setCharPref("general.useragent.override", "{user_agent}");
        prefs.setCharPref("browser.download.dir", "{download_path}");
'''.format(download_path=download_path,user_agent=user_agent)
        self.driver.execute_script(js)
        time.sleep(0.1)
        self.driver.get("about:preferences")
        time.sleep(0.5)
        pdf = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@id="handlersView"]//*[@type="application/pdf"]//*[@class="actionsMenuContainer"]//*[@class="actionsMenu"]'))
        )
        pdf.click()
        time.sleep(0.5)
        pdf.click()
        time.sleep(0.5)
        #pdf.send_keys(Keys.UP)
        pdf.send_keys(Keys.DOWN)
        pdf.send_keys(Keys.DOWN)
        pdf.send_keys(Keys.ENTER)
        time.sleep(0.2)
        #self.loginEmail()
    def test_init(self):
        #人工步骤
        while True:
            self.init_start()
            #break
            if not self.company.get('name',''):
                break
            self.datas.append(self.company)
            self.datas1.append(self.company)
            self.company = {
                "name":""
            }
            self.index = self.index+1
        self.index = 0
        #自动步骤
        while True:
            self.init_start1()
            #break
            if not self.company.get('name',''):
                break
            self.company = {
                "name":""
            }
            self.index = self.index+1
        self.index = 0
        #人工步骤
        while True:
            self.init_start2()
            #break
            if not self.company.get('name',''):
                break
            self.company = {
                "name":""
            }
            self.index = self.index+1
    def init_start1(self):
        self.getCompany1()#获取公司信息
        self.checking1()#执行检查
        try:
            self.fail_num=0
        except Exception as err:
            print(err)
            self.fail_num=self.fail_num+1
            print('连续失败'+str(self.fail_num)+'次!')
        #连续失败1分钟后再试
        if self.fail_num>=self.fail_allow:
            time.sleep(5)
    def init_start2(self):
            self.getCompany2()#获取公司信息
            self.checking2()#执行检查
    def init_start(self):
        self.getCompany()#获取公司信息
        self.mkAllDir()
        self.checking()#执行检查
        try:
            self.fail_num=0
        except Exception as err:
            print(err)
            self.fail_num=self.fail_num+1
            print('连续失败'+str(self.fail_num)+'次!')
        #连续失败1分钟后再试
        if self.fail_num>=self.fail_allow:
            time.sleep(5)
    def checking2(self):
        if self.company.get('name','') and self.driver:
            self.word()
            print('执行完成!')
        else:
            if not self.company.get('name',''):
                time.sleep(random.randint(0,50)/10)
            else:
                time.sleep(1)
    def checking1(self):
        if self.company.get('name','') and self.driver:
            self.step0_1()
            self.step1()
            #站点问题暂时注释
            #self.step2()
            self.step3()
            ##self.step0_2()
            print('执行完成!')
        else:
            if not self.company.get('name',''):
                time.sleep(random.randint(0,50)/10)
            else:
                time.sleep(1)
    def checking(self):
        if self.company.get('name','') and self.driver:
            self.step0()
            print('执行完成(必须手工操作)!')
        else:
            if not self.company.get('name',''):
                time.sleep(random.randint(0,50)/10)
            else:
                time.sleep(1)
    #生成word文档
    def word(self):
        #打开文档
        file=download_path+'/'+self.company['name']+'/信用中国查询截图/'+self.company['name']+'.pdf'
        dir=download_path+'/'+self.company['name']+'/信用中国查询截图/'+self.company['name']
        doc=download_path+'/'+self.company['name']+'/'+self.company['name']+'.docx'
        if os.path.exists(file) and not os.path.exists(dir):
            pyperclip.copy(file)
            pyautogui.hotkey('winleft','e')
            #跳转到对应目录
            time.sleep(2)
            pyautogui.hotkey('tab')
            time.sleep(0.5)
            pyautogui.hotkey('tab')
            time.sleep(0.5)
            pyautogui.hotkey('tab')
            pyautogui.hotkey('enter')
            time.sleep(0.5)
            pyautogui.hotkey('ctrlleft','v')
            time.sleep(0.5)
            pyautogui.hotkey('enter')
            time.sleep(8)
            pyautogui.hotkey('ctrlleft','shiftleft','i')
            time.sleep(3)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('left')
            time.sleep(0.1)
            pyautogui.hotkey('tab')
            time.sleep(0.1)
            num = random.randint(0,2)
            counter = 1
            while counter <= num:
                pyautogui.hotkey('left')
                time.sleep(0.1)
                counter += 1
            pyautogui.keyDown('enter')
            time.sleep(0.1)
            pyautogui.keyUp('enter')
            time.sleep(0.1)
            pyautogui.hotkey('enter')
            while (not os.path.exists(dir)):
                time.sleep(1)
            time.sleep(15)
            #pyautogui.hotkey('altleft','f4')
            #time.sleep(0.5)
            #pyautogui.hotkey('altleft','f4')
            #time.sleep(0.5)
            pyautogui.hotkey('altleft','f4')
            time.sleep(0.5)
        #新建word
        if os.path.exists(doc):
            return
        document = Document()
        #循环遍历目录中的图片
        for name in ['国家企业信用信息公司系统查询','信用中国查询截图','中国裁判文书网','中国政府采购网','小微企业查询']:
            handle_dir = download_path+'/'+self.company['name']+'/'+name
            if not os.path.exists(handle_dir):
               continue
            filenames=os.listdir(handle_dir)
            for filename in filenames:
                if not filename.endswith('.png'):
                    continue
                document.add_picture(handle_dir+'/'+filename,width=Cm(17), height=Cm(23))
            if name=='信用中国查询截图':
                handle_dir=handle_dir+'/'+self.company['name']
                if not os.path.exists(handle_dir):
                    continue
                filenames=os.listdir(handle_dir)
                for filename in filenames:
                    if not filename.endswith('.png'):
                        continue
                    document.add_picture(handle_dir+'/'+filename,width=Cm(17), height=Cm(23))
        document.save(doc)
    def openUrl(self,url,index):
        self.driver.switch_to.window(self.driver.window_handles[index])
        self.driver.get(url)
    def submit(self,index):
        self.driver.switch_to.window(self.driver.window_handles[index])
        #输入搜索
        search_input = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@class="searchBox"]/input'))
        )
        search_input.send_keys(self.company['name'])
        time.sleep(0.1)
        #提交搜索
        search_input.send_keys(Keys.ENTER)
    def clearAgent(self):
        self.driver.get("about:config")
        body = self.driver.find_element(by='css selector', value='body')
        warningButton = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.ID,"warningButton"))
        )
        warningButton.click()
        time.sleep(0.1)
        self.host = ''
        self.port = 0
        js='''
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("network.proxy.type", 5);
prefs.setCharPref("network.proxy.http", "{host}");
prefs.setIntPref("network.proxy.http_port", {port});
prefs.setCharPref("network.proxy.ssl", "{host}");
prefs.setIntPref("network.proxy.ssl_port", {port});
prefs.setCharPref("network.proxy.ftp", "{host}");
prefs.setIntPref("network.proxy.ftp_port", {port});
'''.format(host=self.host,port=self.port)
        self.driver.execute_script(js)
        time.sleep(0.1)
    def setAgent(self):
        agent = self.config.get('agent','')
        if agent:
            response = requests.get(agent)
            if response.status_code != 200:
                return ''
            if not response.json():
                return ''
            if not response.json()['data']:
                time.sleep(3)
                return ''
            list = response.json()['data']['proxy_list']
            if not len(list):
                if proxy:
                    return proxy
                return ''
            index = random.randint(0,len(list)-1)
            proxy = list[index]
            if proxy:
                proxy_info = proxy.split(':')
                self.host = proxy_info[0]
                self.port = proxy_info[1]
                res = "http://{}".format(proxy)
                print(res)
                self.driver.get("about:config")
                body = self.driver.find_element(by='css selector', value='#preferences-body')
                warningButton = WebDriverWait(self.driver,10).until(
                    EC.presence_of_element_located((By.ID,"warningButton"))
                )
                warningButton.click()
                time.sleep(0.1)
                js='''
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "{host}");
prefs.setIntPref("network.proxy.http_port", {port});
prefs.setCharPref("network.proxy.ssl", "{host}");
prefs.setIntPref("network.proxy.ssl_port", {port});
prefs.setCharPref("network.proxy.ftp", "{host}");
prefs.setIntPref("network.proxy.ftp_port", {port});
'''.format(host=self.host,port=self.port)
                self.driver.execute_script(js)
                time.sleep(0.1)
                return res
            return ''
    def mkAllDir(self):
        if self.company['name']:
            dir = download_path+'/'+self.company['name']
            self.mkdir(dir)
            for name in ['信用中国查询截图','中国裁判文书网','中国政府采购网','小微企业查询','国家企业信用信息公司系统查询']:
                self.mkdir(dir+'/'+name)
    # 查询小微企业
    def step0(self):
        driver=self.driver
        #进入首页
        self.openUrl(url=self.xwqy_url,index=0)
        input = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"searchtitle"))
        )
        input.send_keys(self.company['name'])
        searchBtn = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".btn_search_ss"))
        )
        searchBtn.click()
        while True:
            time.sleep(0.5)
            if self.isElementPresent("css selector", '.gt_show .gt_popup_wrap'):
               break
        while True:
            time.sleep(0.5)
            if not self.isElementPresent("css selector", '.gt_show .gt_popup_wrap'):
               break
        tbody = WebDriverWait(driver,60*3).until(
            EC.presence_of_element_located((By.ID,"tbody"))
        )
        nameEl = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,"div.res_qy_item:nth-child(1) > div:nth-child(1) > span:nth-child(1)"))
        )
        if not (nameEl.text.strip()==self.company['name']):
            return
        time.sleep(1)
        self.jieTu()
        time.sleep(2)
        aEl = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,"#tbody .res_qy_title"))
        )
        aEl.click()
        time.sleep(2)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.close()
        time.sleep(0.5)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.jieTu()
        time.sleep(2)
        self.move(dir_name='小微企业查询')
    def step0_1(self):
        self.setAgent()
        driver = self.driver
        self.driver.get('https://www.creditchina.gov.cn/xinyongxinxi/index.html?index=0&scenes=defaultScenario&tableName=credit_xyzx_tyshxydm&searchState=2&entityType=1,2,4,5,6,7,8&keyword='+self.company['name'])
        time.sleep(0.5)
        company_item='.company-item'
        while True:
            time.sleep(0.5)
            if self.isElementPresent("css selector",company_item):
               time.sleep(1)
               break
        aEl = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,company_item))
        )
        aEl.click()
        time.sleep(2)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.close()
        time.sleep(2)
        self.driver.switch_to.window(self.driver.window_handles[0])
        time.sleep(3)
        print('加载完成1')
        btn1 = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"permissionBtn"))
        )
        btn1.click()
        self.jieTu()
        time.sleep(2)
        #发送报告
        btn_send = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".download"))
        )
        btn_send.click()
        print('下载信用报告')
        #检查文件是否下载完成
        while True:
            time.sleep(0.5)
            filename = download_path+'/'+self.company['name']+'.pdf'
            if os.path.exists(filename) and os.path.isfile(filename) and not os.path.exists(filename+'.part'):
               break
        time.sleep(1)
        self.clearAgent()
        self.move(dir_name='国家企业信用信息公司系统查询')
    #发送成功下载邮箱报告
    def step0_2(self):
        #if self.login_email_ed==0:
        #    self.loginEmail()
        driver = self.driver
        self.driver.get(self.config['notice'].get('url','https://mail.qq.com/'))
        folder_1 = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"folder_1"))
        )
        folder_1.click()
        iframe = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.ID,'mainFrame'))
        )
        self.driver.switch_to.frame(iframe)
        time.sleep(1)
        print('等待邮件')
        self.company['name'] = '四川恒泰环境技术有限责任公司'
        email_content = WebDriverWait(driver,60*10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@id="div_showtoday"]//b[contains(text(), "'+self.company['name']+'")]'))
        )
        email = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@id="div_showtoday"]/table[1]/tbody/tr/td[3]'))
        )
        try:
            email.click()
        except:
            pass
        down = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,'/html/body/div/div[2]/div[5]/div[2]/div[2]/div[2]/div/a[2]'))
        )
        down.click()
        time.sleep(2)
        quick_del = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,'quick_del'))
        )
        quick_del.click()
        time.sleep(1)
        self.driver.switch_to.default_content()
        time.sleep(0.5)
        self.move(dir_name='国家企业信用信息公司系统查询')
    def step1(self):
        driver=self.driver
        self.driver.get("about:config")
        body = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.TAG_NAME,"body"))
        )
        for url in self.urls:
            body.send_keys(Keys.CONTROL + 't')
            body.send_keys(Keys.COMMAND + 't')
            time.sleep(0.5)
        #进入首页
        self.openUrl(url=self.credit_home_url,index=0)
        index = 0
        for url in self.urls:
            index = index+1
            self.openUrl(url=url,index=index)
        self.driver.switch_to.window(self.driver.window_handles[0])
        #输入搜索
        search_input = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"search_input"))
        )
        search_input.send_keys(self.company['name'])
        index = 0
        for url in self.urls:
            index = index+1
            self.submit(index=index)
        self.driver.switch_to.window(self.driver.window_handles[0])
        #提交搜索
        search_input.send_keys(Keys.ENTER)
        time.sleep(3)
        self.driver.switch_to.window(self.driver.window_handles[1])
        resultBox = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@id="content"]//*[@class="search-result-box"]'))
        )
        if resultBox.is_displayed() is False:
            self.company['msg'] = '公司名称可能错误,未查询到结果'
            return
        #点击搜索结果
        item1 = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@id="companylists"]/li[1]'))
        )
        item1.click()
        self.driver.switch_to.window(self.driver.window_handles[2])
        #下载信用报告
        download = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@class="download"]'))
        )
        self.jieTu()
        time.sleep(3)
        dir = download_path+'/'+self.company['name']
        filename = dir+'.pdf'
        if not os.path.isfile(filename):
            download.click()
        while True:
            time.sleep(0.5)
            if os.path.exists(filename) and os.path.isfile(filename) and not os.path.exists(filename+'.part'):
               break
        time.sleep(1)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.close()
        time.sleep(0.5)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.close()
        time.sleep(0.5)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.close()
        time.sleep(0.5)
        #截图搜索结果
        index = 0
        for url in self.urls:
            index = index+1
            self.driver.switch_to.window(self.driver.window_handles[0])
            time.sleep(0.1)
            self.jieTu()
            if index!=len(self.urls):
                self.driver.close()
                time.sleep(0.5)
        time.sleep(2)
        #移动文件
        self.move(dir_name='信用中国查询截图')
    def step2(self):
        driver=self.driver
        #进入首页
        self.openUrl(url=self.ccgp_url,index=0)
        iframe = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.TAG_NAME,'iframe'))
        )
        self.driver.switch_to.frame(iframe)
        search_input = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"orgName"))
        )
        search_input.send_keys(self.company['name'])
        searchForm = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,"searchForm"))
        )
        searchForm.click()
        time.sleep(3)
        self.driver.switch_to.default_content()
        time.sleep(0.3)
        self.jieTu()
        time.sleep(2)
        self.move(dir_name='中国政府采购网')
    def step3(self):
        if self.login_wen_shu_ed==0:
            self.loginWenShu()
        driver=self.driver
        #进入首页
        self.openUrl(url=self.wenshu_url,index=0)
        time.sleep(2)
        souye = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".souye"))
        )
        souye.click()
        time.sleep(2)
        self.driver.close()
        time.sleep(2)
        self.driver.switch_to.window(self.driver.window_handles[0])
        input = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".searchKey"))
        )
        input.click()
        time.sleep(1)
        input.send_keys(self.company['name'])
        time.sleep(1)
        search = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,".search-click"))
        )
        search.click()
        #input.send_keys(Keys.ENTER)
        try:
            listText = WebDriverWait(driver,20).until(
                EC.presence_of_element_located((By.CSS_SELECTOR,".LM_left .treeItem"))
            )
            if self.index<len(self.config['show_phone']):
                phone_num = self.config['show_phone'][random.randint(0,len(self.config['show_phone']))]
                loginLi = WebDriverWait(driver,10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,"#loginLi"))
                )
                text = loginLi.text.replace(self.config['wen_shu'].get('username',''),phone_num)
                js = '''
    document.getElementById('loginLi').innerHTML="{text}"
    '''.format(text=text)
                self.driver.execute_script(js)
        except:
            pass
        self.jieTu()
        time.sleep(2)
        self.move(dir_name='中国裁判文书网')

    def move(self,dir_name):
        #移动文件
        files = sorted(os.listdir(download_path))
        for file in files:
            if file.startswith('.') or os.path.isdir(download_path+'/'+file):
                continue
            shutil.move(download_path+'/'+file, download_path+'/'+self.company['name']+'/'+dir_name+'/'+file)
    def mkdir(self,dir):
        if not os.path.exists(dir):
            os.makedirs(dir)
    def jieTu(self):
        driver = self.driver
        body = self.driver.find_element(by='css selector', value='body')
        rightClick = ActionChains(self.driver)  # 实例化ActionChains类
        rightClick.context_click(body).perform()  # context_click(logo)在logo上执行右键操作，perform()是一个执行动作
        time.sleep(0.1)
        if platform.system()=='Windows':
            pyautogui.typewrite(['up','up','up'])
        else:
            pyautogui.typewrite(['down','down','down','down'])  # 选中右键菜单中第2个选项
            pyautogui.typewrite(['down','down','down','down'])
            pyautogui.typewrite(['down','down','down','down'])
            pyautogui.typewrite(['down','down','down','down'])
        time.sleep(0.1)
        pyautogui.typewrite(['enter','return'])  # 最后一个按键： mac电脑用的return，Windows应用enter
        while True:
            time.sleep(0.5)
            if self.isElementPresent("css selector", '#firefox-screenshots-preselection-iframe'):
               time.sleep(1)
               break
        imgBtnIframe = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,'firefox-screenshots-preselection-iframe'))
        )
        self.driver.switch_to.frame(imgBtnIframe)
        fullPageBtn = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@class="full-page"]'))
        )
        fullPageBtn.click()
        self.driver.switch_to.default_content()
        time.sleep(3)
        imgBtnIframe1 = WebDriverWait(driver,10).until(
            EC.presence_of_element_located((By.ID,'firefox-screenshots-preview-iframe'))
        )
        self.driver.switch_to.frame(imgBtnIframe1)
        fullPageBtn = WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.XPATH,'//*[@class="highlight-button-download"]'))
        )
        fullPageBtn.click()
        self.driver.switch_to.default_content()
        time.sleep(0.5)
    def getCompany1(self):
        if len(self.datas):
            self.company = self.datas.pop(0)
    def getCompany2(self):
        if len(self.datas1):
            self.company = self.datas1.pop(0)
    #爬取数据前先检查用户名信息
    def getCompany(self):
        file_name = self.file_name
        if not os.path.exists(file_name):
            return
        with open(file_name, 'r', encoding='utf-8') as f:  # 打开文件
            first_line = f.readline().strip()  # 取第一行
            all=''.join(f.readlines()[0:])
            f.close()
            if first_line or all:
                with open(file_name, 'w', encoding='utf-8') as fw:
                    fw.write(all)
                    fw.close()
                os.chmod(file_name,stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
            if first_line:
                print('数据源:'+first_line)
                self.company['name'] = first_line.strip()
        if not first_line:
           return
    def isElementPresent(self, by, value):
        try:
            element = ''
            try:
                if by=='class':
                    element = self.driver.find_element_by_css_selector(value)
                else:
                    element = self.driver.find_element(by=by, value=value)
            except NoSuchElementException as e:
                # 打印异常信息
                #print(e)
                # 发生了NoSuchElementException异常，说明页面中未找到该元素，返回False
                return False
            else:
                # 没有发生异常，表示在页面中找到了该元素，返回True
                if not element:
                   return False
                return True
        except:
            pass
        return False
    def get_value(self,xpath,type):
        res = self.isElementPresent("xpath", xpath)
        if res is True:
            if type=='text':
                return self.driver.find_element(By.XPATH,xpath).text
            else:
                return self.driver.find_element(By.XPATH,xpath).get_attribute("value")
        return ''
    #退出关闭浏览器
    def tearDown(self):
        if self.driver:
            try:
                pass
                #self.driver.quit()
            except:
                 print('关闭浏览器异常')
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', help='input file', dest='inputfile')
    ns, args = parser.parse_known_args(namespace=unittest)
    #args = parser.parse_args()
    return ns, sys.argv[:1] + args

if __name__ == '__main__':
    import sys
    args, argv = parse_args()   # run this first
    #print(args, argv)
    sys.argv[:] = argv       # create cleans argv for main()
    unittest.main()
